package com.eastmoney.gamecode.vo;

import com.eastmoney.gamecode.OutOfMapException;
import com.eastmoney.gamecode.constant.GameConstants;
import com.eastmoney.gamecode.enums.MoveTypeEnums;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 坐标
 *
 * @author Liang Desheng
 * @version 1.0
 * @since 2023/10/1 10:31
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Loc {
    public static  int MAP_X_LIMIT = 280;
    public static  int MAP_Y_LIMIT = 172;

    public static final Set<Loc> DIRECTIONS = new HashSet<>();
    public static final Set<Loc> DIRECTIONS_N = new HashSet<>();

    public static final Map<String, MoveTypeEnums> DIREC_MAP = new HashMap<>();

    static {
        DIRECTIONS.add(Loc.of(1, 0));
        DIRECTIONS.add(Loc.of(-1, 0));
        DIRECTIONS.add(Loc.of(0, 1));
        DIRECTIONS.add(Loc.of(0, -1));


        DIRECTIONS_N.add(Loc.of(GameConstants.DIS, 0));
        DIRECTIONS_N.add(Loc.of(-GameConstants.DIS, 0));
        DIRECTIONS_N.add(Loc.of(0, GameConstants.DIS));
        DIRECTIONS_N.add(Loc.of(0, -GameConstants.DIS));

        DIREC_MAP.put(String.format("%s,0", GameConstants.DIS), MoveTypeEnums.RIGHT);
        DIREC_MAP.put(String.format("-%s,0", GameConstants.DIS), MoveTypeEnums.LEFT);
        DIREC_MAP.put(String.format("0,%s", GameConstants.DIS), MoveTypeEnums.DOWN);
        DIREC_MAP.put(String.format("0,-%s", GameConstants.DIS), MoveTypeEnums.TOP);
    }

    public int x;
    public int y;

    public static Loc of(int x, int y) {
        return new Loc(x, y);
    }

    public Loc move(Loc direction) {
        Loc newLoc = Loc.of(x + direction.x, y + direction.y);
        if (newLoc.x >= MAP_X_LIMIT || newLoc.x < 0) {
            throw OutOfMapException.X_OUT;
        } else if (newLoc.y >= MAP_Y_LIMIT || newLoc.y < 0) {
            throw OutOfMapException.Y_OUT;
        }
        return newLoc;
    }

    @Override
    public String toString() {
        return String.format("(x=%s, y=%s)", x, y);
    }
}
